package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServiceResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by xinxuchu on 2018/10/9.
 */
@Controller
@RequestMapping("/user/")
public class UserController {


    @Autowired
    private IUserService iUserService;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @param session
     * @return
     */

    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<User> login(String username, String password, HttpSession session) {
        //这个ServiceResponse高复用的接口类 由状态位，message，data组成
        ServiceResponse<User> response = iUserService.login(username,password);
        if (response.isSuccess()){
           session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    /**
     * 用户登出
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<User> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServiceResponse.createBySuccess("退出成功！");
    }


    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> register(User user){
        return iUserService.register(user);
    }

    /**
     * 因为需要频繁的使用到用户注册中的一段代买逻辑
     * 测试用户名是否存在，测试邮箱是否存在
     * @param str
     * @param type
     * @return
     */
    @RequestMapping(value = "check_Valid.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> checkValid (String str,String type){
        return iUserService.checkValid(str, type);
    }


    /**
     * 获取用户登录信息
     * @param session
     * @return
     */
    @RequestMapping(value = "getUser_Info.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<User> getUserInfo(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user!=null){
            return ServiceResponse.createBySuccess(user);
        }
        return ServiceResponse.createByError("用户未登录，无法获取当前用户信息！");
    }

    /**
     * 找回密碼，獲取密保問題信息
     * @param username
     * @return
     */
    @RequestMapping(value = "forget_get_question.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> forgetGetQuestion(String username){
        return iUserService.selectQuestion(username);
    }


    /**
     * 校验问题答案
     * 利用Guava本地缓存保存当前校验状态，并设置存储时间
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value = "forget_check_answer.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> forgetCheckanswer(String username, String question, String answer) {
        return iUserService.checkAnswer(username,question,answer);
    }

    /**
     * 忘记密码中的重置密码
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
    @RequestMapping(value = "forget_rest_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> forgetRestPassword(String username, String passwordNew, String forgetToken){
        return iUserService.forgetRestPassword(username,passwordNew,forgetToken);
    }


    /**
     * 登录状态状态下的重置密码
     * @param session
     * @param passwordOld
     * @param passwordNew
     * @return
     */
    @RequestMapping(value = "rest_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> resetPassword (HttpSession session,String passwordOld,String passwordNew){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServiceResponse.createByError("用户未登录！");
        }
        return iUserService.resetPassword(passwordOld,passwordNew,user);
    }

    /**
     * 更新个人用户信息
     * @param session
     * @param user
     * @return
     */
    @RequestMapping(value = "update_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<User> update_information(HttpSession session,User user){

        User CurrentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (CurrentUser==null){
            return ServiceResponse.createByError("用户未登录！");
        }
        user.setId(CurrentUser.getId());
        user.setUsername(CurrentUser.getUsername());
        ServiceResponse<User> response = iUserService.updateInfomation(user);
        if (response.isSuccess()){
            response.getData().setUsername(CurrentUser.getUsername());
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }


    /**
     * 获取登录当前信息，若未登录需要强制登录
     * @param session
     * @return
     */
    @RequestMapping(value = "get_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<User> get_information(HttpSession session){
        //未登录，需要强制登录
        User CurrentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (CurrentUser==null){
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要强制登录！state = 10");
        }
        return iUserService.getInformation(CurrentUser.getId());
    }
}
